package com.gxuwz.zhangjiahui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.zhangjiahui.entity.Limit;
import com.gxuwz.zhangjiahui.entity.Role;
import com.gxuwz.zhangjiahui.entity.User;
import com.gxuwz.zhangjiahui.service.ILimitService;
import org.apache.ibatis.annotations.Param;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangjiahui
 * @since 2021-06-20
 */
@RestController
@RequestMapping("/limit")
public class LimitController {

    @Autowired
    private ILimitService iLimitService;


    /**
     * 查询全部权限信息
     * @param modelAndView
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @Pointcut
    @ResponseBody
    @RequestMapping(value = "/findLimit")
    public ModelAndView findLimit(ModelAndView modelAndView, Page page, Integer pageNumber, User user,
                                 @Param("limitText") String limitText,
                                 HttpServletRequest request, HttpServletResponse response) {
        if(request.getSession().getAttribute("nowResult") != null){
            if (!request.getSession().getAttribute("nowResult").equals("now")) {
                request.getSession().setAttribute("result", "");
            }
            request.getSession().setAttribute("nowResult", "");
        }
        // 可以通过 wrapper 进行筛选!!!
        QueryWrapper<Limit> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        // 进行模糊查询!!!
        if(limitText != null && limitText != ""){
            wrapper.like("limit_text", limitText);
            request.getSession().setAttribute("limitText", limitText);
        }else {
            request.getSession().setAttribute("limitText", "");
        }
        // Current,页码 + Size,每页条数
        if(pageNumber == null){
            page.setCurrent(1);
        }else {
            page.setCurrent((long)pageNumber);
        }
        // 下拉框选择器
        if(user != null & user.getRoleId() != null){
            if(!user.getRoleId().equals("1000")){
                wrapper.eq("role_id", user.getRoleId());
            }
        }
        // 默认每页6行数据！
        page.setSize(6);
        // 调用分页查询方法！!
        IPage<Limit> limitIPage = iLimitService.selectLimitPage(page, wrapper);
        // 存放page，内有当前页数
        modelAndView.addObject("page", page);
        System.out.println("总条数"+limitIPage.getTotal());
        System.out.println("总页数"+limitIPage.getPages());
        // 存放总页数
        modelAndView.addObject("pages", (int)limitIPage.getPages());
        // 存放一个数组用来让foreach遍历
        int[] pagesList = new int[(int)limitIPage.getPages()];
        for(int i=0; i< (int)limitIPage.getPages(); i++){
            pagesList[i] = i+1;
        }
        modelAndView.addObject("pagesList", pagesList);
        modelAndView.addObject("numberPages", limitIPage.getTotal());
        List<Limit> limitList = limitIPage.getRecords();
        System.out.println("limitList = "+limitList);
        modelAndView.addObject("limitList", limitList);

        modelAndView.setViewName("limit/limit_list");
        return modelAndView;
    }



    /**
     * 添加权限信息 / 修改权限信息
     * @param limit
     * @return
     */
    @PostMapping(value = "/addEditLimit")
    public ModelAndView addEditLimit(Limit limit, HttpServletRequest request, ModelAndView modelAndView) {
        Limit limit1 = iLimitService.findLimitById(limit);
        System.out.println("limit = " + limit);
        System.out.println("limit1 = " + limit1);
        // 新增权限信息
        if(limit1 == null){
            System.out.println("进入新增权限");
            try {
                iLimitService.addLimit(limit);
                request.getSession().setAttribute("result", "addTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "addFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        // 修改权限信息
        if(limit1 != null){
            System.out.println("进入修改权限");
            try {
                iLimitService.updateLimitById(limit);
                request.getSession().setAttribute("result", "editTrue");
                request.getSession().setAttribute("nowResult", "now");
            }catch (Exception e){
                request.getSession().setAttribute("result", "editFalse");
                request.getSession().setAttribute("nowResult", "now");
            }
        }
        modelAndView.setViewName("redirect:/limit/findLimit");
        return modelAndView;
    }



    /**
     * 删除用户信息
     * @param limitId
     * @return
     */
    @GetMapping(value = "/deleteLimit")
    public ModelAndView deleteLimit(HttpServletRequest request, ModelAndView modelAndView,
                              @Param("limitId") String limitId) {
        Limit limit = new Limit();
        limit.setLimitId(limitId);
        try {
            iLimitService.deleteLimitById(limit);
            request.getSession().setAttribute("result", "deleteTrue");
            request.getSession().setAttribute("nowResult", "now");
        }catch (Exception e){

        }
        modelAndView.setViewName("redirect:/limit/findLimit");
        return modelAndView;
    }

}
